package io.github.maojian.doublepointer;

import org.junit.Test;

import java.util.Arrays;

/**
 * @Auther: jian.mao
 * @Date: 2022/6/17 09:26
 * @Description: https://leetcode.cn/problems/duplicate-zeros/
 */
public class Q1089 {
    public void duplicateZeros(int[] arr) {
        int n = arr.length;
        int top = 0;
        int i = -1;
        while (top < n) {
            i++;
            if (arr[i] != 0) {
                top++;
            } else {
                top += 2;
            }
        }
        int j = n - 1;
        if (top == n + 1) {
            arr[j] = 0;
            j--;
            i--;
        }
        while (j >= 0) {
            arr[j] = arr[i];
            j--;
            if (arr[i] == 0) {
                arr[j] = arr[i];
                j--;
            }
            i--;
        }
        Arrays.stream(arr).forEach(System.out::print);
    }


    @Test
    public void test() {
        int[] arr01 = new int[]{1, 5, 2, 0, 6, 8, 0, 6, 0};
        duplicateZeros(arr01);
    }
}
